package team.hyznrj.studentsys.converter;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.hyznrj.studentsys.dao.mapper.ConductScoreMapper;
import team.hyznrj.studentsys.dao.repository.*;
import team.hyznrj.studentsys.dto.ConductScoreDto;
import team.hyznrj.studentsys.dto.QualityMarkDto;
import team.hyznrj.studentsys.dto.SkillMarkDto;
import team.hyznrj.studentsys.entity.*;
import team.hyznrj.studentsys.entity.table.ScoreQualityTable;
import team.hyznrj.studentsys.entity.table.ScoreQualityTableKey;
import team.hyznrj.studentsys.entity.table.ScoreSkillTable;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ReMidDream
 * @date 2018-04-09 15:19
 **/
@Component
public class ConductScore2ConductScoreDto {

    @Autowired
    private OrganizeMarkRepository organizeMarkRepository;
    @Autowired
    private SpecialMarkRepository specialMarkRepository;
    @Autowired
    private ScienceMarkRepository scienceMarkRepository;
    @Autowired
    private SkillMarkRepository skillMarkRepository;
    @Autowired
    private ConductScoreMapper conductScoreMapper;
    @Autowired
    private QualityMarkRepository qualityMarkRepository;
    @Autowired
    private AcademicYearsRepository academicYearsRepository;

    public List<ConductScoreDto> converter(List<ConductScore> conductScoreList){
        //将 操行分的Id 和 学年的Id 提取出来
        Set<String> ids = conductScoreList.stream().map(ConductScore::getId).collect(Collectors.toSet());
        Set<Integer> years = conductScoreList.stream().map(ConductScore::getAcademicYear).collect(Collectors.toSet());

        //根据Id获取数据
        List<AcademicYears> academicYears = academicYearsRepository.findAll(years);
        List<ScienceMark> scienceMarks = scienceMarkRepository.findAll(ids);
        List<SpecialMark> specialMarks = specialMarkRepository.findAll(ids);
        List<OrganizeMark> organizeMarks = organizeMarkRepository.findAll(ids);

        List<ScoreQualityTable> scoreQualityTables = conductScoreMapper.findScoreQualityTableByIds(ids);
        List<ScoreSkillTable> scoreSkillTables = conductScoreMapper.findScoreSkillTableByIds(ids);

        //将 综合素质表的Id 和 技术技能分的Id 提取出来
        Set<String> qualityMarkIds = scoreQualityTables.stream().map(ScoreQualityTable::getQualityMarkId).collect(Collectors.toSet());
        Set<String> skillMarkIds = scoreSkillTables.stream().map(ScoreSkillTable::getSkillMarkId).collect(Collectors.toSet());

        //根据Id获取数据
        List<SkillMark> skillMarks = skillMarkRepository.findAll(skillMarkIds);
        List<QualityMark> qualityMarks = qualityMarkRepository.findAll(qualityMarkIds);

        return  conductScoreList.stream().map(conductScore ->{
            ConductScoreDto conductScoreDto = new ConductScoreDto();
            List<QualityMarkDto> qualityMarkDtos = new ArrayList<>();
            List<SkillMarkDto> skillMarkDtos = new ArrayList<>();
            //拼接学年
            for (AcademicYears year : academicYears) {
                if (year.getId().equals(conductScore.getAcademicYear())) {
                    conductScoreDto.setAcademicYear(year.getAcademicYear());
                }
            }
            BeanUtils.copyProperties(conductScore,conductScoreDto);
            //拼接科学技能分
            for (ScienceMark scienceMark : scienceMarks) {
                if (scienceMark.getConductScoreId().equals(conductScore.getId())) {
                    if (scienceMark.getScienceMarkValue()==null) {
                        scienceMark.setScienceMarkValue(new BigDecimal(0.00));
                    }
                    conductScoreDto.setScienceMark(scienceMark);
                    break;
                }
            }
            //拼接组织管理分
            for (OrganizeMark organizeMark : organizeMarks) {
                if (organizeMark.getConductScoreId().equals(conductScore.getId())) {
                    if (organizeMark.getOrganizeMarkValue()==null) {
                        organizeMark.setOrganizeMarkValue(new BigDecimal(0.00));
                    }
                    conductScoreDto.setOrganizeMark(organizeMark);
                    break;
                }
            }
            //拼接特殊加分
            for (SpecialMark specialMark : specialMarks) {
                if (specialMark.getConductScoreId().equals(conductScore.getId())) {
                    if (specialMark.getSpecialMarkValue()==null) {
                        specialMark.setSpecialMarkValue(new BigDecimal(0.00));
                    }
                    conductScoreDto.setSpecialMark(specialMark);
                    break;
                }
            }
            //拼接综合素质分
            for (ScoreQualityTable scoreQualityTable : scoreQualityTables) {
                if (scoreQualityTable.getConductScoreId().equals(conductScore.getId())) {
                    for (QualityMark qualityMark : qualityMarks) {
                        if (qualityMark.getId().equals(scoreQualityTable.getQualityMarkId())) {
                            if (scoreQualityTable.getMark()==null) {
                                scoreQualityTable.setMark(new BigDecimal(0.00));
                            }
                            qualityMarkDtos.add(qualityCopy(qualityMark,scoreQualityTable));
                            break;
                        }
                    }
                }
            }
            conductScoreDto.setQualityMark(qualityMarkDtos);
            //拼接技术技能分
            for (ScoreSkillTable scoreSkillTable : scoreSkillTables) {
                if (scoreSkillTable.getConductScoreId().equals(conductScore.getId())) {
                    for (SkillMark skillMark : skillMarks) {
                        if (skillMark.getId().equals(scoreSkillTable.getSkillMarkId())) {
                            if (scoreSkillTable.getMark()==null) {
                                scoreSkillTable.setMark(new BigDecimal(0.00));
                            }
                            skillMarkDtos.add(skillCopy(skillMark,scoreSkillTable));
                            break;
                        }
                    }
                }
            }
            conductScoreDto.setSkillMark(skillMarkDtos);
           return conductScoreDto;
        }).collect(Collectors.toList());
    }

    private SkillMarkDto skillCopy(SkillMark skillMark,ScoreSkillTable scoreSkillTable){
        SkillMarkDto skillMarkDto = new SkillMarkDto();
        BeanUtils.copyProperties(skillMark,skillMarkDto);
        BeanUtils.copyProperties(scoreSkillTable,skillMarkDto);
        return skillMarkDto;
    }

    private QualityMarkDto qualityCopy(QualityMark qualityMark,ScoreQualityTable qualityTable){
        QualityMarkDto qualityMarkDto = new QualityMarkDto();
        BeanUtils.copyProperties(qualityMark,qualityMarkDto);
        BeanUtils.copyProperties(qualityTable,qualityMarkDto);
        return qualityMarkDto;
    }


}
